# coding:utf-8
# 房间管理器

import gnet
import room

# 房间数据
data = {}

# 记录next_id
global next_id
next_id = 1

# 初始化
def init():
	print "room_mgr>init"
	

def create_room (name, mode):
	print "room_mgr>create_room", name, mode

	room_id = get_next_id()
	name = name
	mode = mode
	max_player = 8
	aroom = room.Room(room_id, name, mode, max_player)
	print "room_mgr>new_room", aroom
	
	# 记录数据
	data[room_id] = aroom
	return room_id


def join_room (player, room_id):
	print "room_mgr>join_room", player, room_id

	aroom = get_room(room_id)
	if aroom == None: return False

	aroom.add_player(player)
	
	return True


def exit_room (player, room_id):
	print "exit_room>join_room", player, room_id

	aroom = get_room(room_id)
	if aroom == None: return False

	aroom.remove_player(player)
	
	return True


def get_room (room_id):
	if data.has_key(room_id):
		return data[room_id]
	else:
		print "room_mgr>get_room() : no this id : ", room_id
		return None


# --------------------
def get_next_id ():
	global next_id
	res = next_id
	next_id += 1
	return res


